package com.investment.util.BaseDao;

/**
 * Created by veicth on 17/2/25.
 */
import com.github.pagehelper.PageHelper;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageInfo;
import com.investment.util.DaoException.DbException;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;

public abstract class BaseReadDao<T> extends MyBatisDao<T> {
    @Autowired
    @Qualifier("masterSqlSessionTemplate")
    private SqlSessionTemplate masterSqlSessionTemplate;
    @Autowired
    protected JdbcTemplate jdbcTemplate;

    public BaseReadDao() {
    }

    private SqlSession getLocalSqlSession(SqlSessionTemplate paramSqlSessionTemplate, boolean paramBoolean) throws DbException {
        return (SqlSession)(paramSqlSessionTemplate == null?this.masterSqlSessionTemplate:paramSqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, paramBoolean));
    }

    public PageInfo<T> paginate() throws DbException {
        return this.paginate((Object)null);
    }

    public PageInfo<T> paginate(Object bean) throws DbException {
        return this.paginate(bean, 0);
    }

    public PageInfo<T> paginate(String sqlId, Object bean) throws DbException {
        return this.paginate(sqlId, bean, 0);
    }

    public PageInfo<T> paginate(Object bean, int pageNum) throws DbException {
        return this.paginate(bean, pageNum, 10);
    }

    public PageInfo<T> paginate(Object bean, int pageNum, int pageSize) throws DbException {
        return this.paginate("list", bean, pageNum, pageSize);
    }

    public PageInfo<T> paginate(String sqlId, Object bean, int pageNum) throws DbException {
        return this.paginate(sqlId, bean, pageNum, 10);
    }

    public PageInfo<T> paginate(String sqlId, Object bean, int pageNum, int pageSize) throws DbException {
        return this.paginate((SqlSessionTemplate)null, sqlId, bean, pageNum, pageSize);
    }

    public PageInfo<T> paginate(SqlSessionTemplate sessionTemplate, String sqlId, Object bean, int pageNum, int pageSize) throws DbException {
        PageHelper.startPage(pageNum, pageSize);

        try {
            SqlSession ex = this.getLocalSqlSession(sessionTemplate, false);
            List list = null;
            if(bean == null) {
                list = ex.selectList(this.formatSqlId(sqlId));
            } else {
                list = ex.selectList(this.formatSqlId(sqlId), bean);
            }

            return new PageInfo(list);
        } catch (Exception var8) {
            throw new DbException(var8);
        }
    }

    public <E> PageInfo<E> paginateBean() throws DbException {
        return this.paginateBean((Object)null);
    }

    public <E> PageInfo<E> paginateBean(Object bean) throws DbException {
        return this.paginateBean(bean, 0);
    }

    public <E> PageInfo<E> paginateBean(String sqlId, Object bean) throws DbException {
        return this.paginateBean(sqlId, bean, 0);
    }

    public <E> PageInfo<E> paginateBean(Object bean, int pageNum) throws DbException {
        return this.paginateBean(bean, pageNum, 10);
    }

    public <E> PageInfo<E> paginateBean(Object bean, int pageNum, int pageSize) throws DbException {
        return this.paginateBean("list", bean, pageNum, pageSize);
    }

    public <E> PageInfo<E> paginateBean(String sqlId, Object bean, int pageNum) throws DbException {
        return this.paginateBean(sqlId, bean, pageNum, 10);
    }

    public <E> PageInfo<E> paginateBean(String sqlId, Object bean, int pageNum, int pageSize) throws DbException {
        return this.paginateBean((SqlSessionTemplate)null, sqlId, bean, pageNum, pageSize);
    }

    public <E> PageInfo<E> paginateBean(SqlSessionTemplate sessionTemplate, String sqlId, Object bean, int pageNum, int pageSize) throws DbException {
        PageHelper.startPage(pageNum, pageSize);

        try {
            SqlSession ex = this.getLocalSqlSession(sessionTemplate, false);
            List list = null;
            if(bean == null) {
                list = ex.selectList(this.formatSqlId(sqlId));
            } else {
                list = ex.selectList(this.formatSqlId(sqlId), bean);
            }

            return new PageInfo(list);
        } catch (Exception var8) {
            throw new DbException(var8);
        }
    }

    public List<T> selectList() throws DbException {
        return this.selectList((Object)null);
    }

    public List<T> selectList(Object bean) throws DbException {
        return this.selectList("list", bean);
    }

    public List<T> selectList(String sqlId, Object bean) throws DbException {
        return this.selectList((SqlSessionTemplate)null, sqlId, bean);
    }

    public List<T> selectList(SqlSessionTemplate sessionTemplate, String sqlId) throws DbException {
        return this.selectList(sessionTemplate, sqlId, (Object)null);
    }

    public List<T> selectList(SqlSessionTemplate sessionTemplate, String sqlId, Object bean) throws DbException {
        try {
            SqlSession ex = this.getLocalSqlSession(sessionTemplate, false);
            return bean == null?ex.selectList(this.formatSqlId(sqlId)):ex.selectList(this.formatSqlId(sqlId), bean);
        } catch (Exception var5) {
            throw new DbException(var5);
        }
    }

    public <E> List<E> selectListBean() throws DbException {
        return this.selectListBean((Object)null);
    }

    public <E> List<E> selectListBean(Object bean) throws DbException {
        return this.selectListBean("list", bean);
    }

    public <E> List<E> selectListBean(String sqlId, Object bean) throws DbException {
        return this.selectListBean((SqlSessionTemplate)null, sqlId, bean);
    }

    public <E> List<E> selectListBean(SqlSessionTemplate sessionTemplate, String sqlId) throws DbException {
        return this.selectListBean(sessionTemplate, sqlId, (Object)null);
    }

    public <E> List<E> selectListBean(SqlSessionTemplate sessionTemplate, String sqlId, Object bean) throws DbException {
        try {
            SqlSession ex = this.getLocalSqlSession(sessionTemplate, false);
            return bean == null?ex.selectList(this.formatSqlId(sqlId)):ex.selectList(this.formatSqlId(sqlId), bean);
        } catch (Exception var5) {
            throw new DbException(var5);
        }
    }

    public List<Map<String, Object>> selectListMap() throws DbException {
        return this.selectListMap((Object)null);
    }

    public List<Map<String, Object>> selectListMap(Object bean) throws DbException {
        return this.selectListMap("list", bean);
    }

    public List<Map<String, Object>> selectListMap(String sqlId, Object bean) throws DbException {
        return this.selectListMap((SqlSessionTemplate)null, sqlId, bean);
    }

    public List<Map<String, Object>> selectListMap(SqlSessionTemplate sessionTemplate, String sqlId) throws DbException {
        return this.selectListMap(sessionTemplate, sqlId, (Object)null);
    }

    public List<Map<String, Object>> selectListMap(SqlSessionTemplate sessionTemplate, String sqlId, Object bean) throws DbException {
        try {
            SqlSession ex = this.getLocalSqlSession(sessionTemplate, false);
            return bean == null?ex.selectList(this.formatSqlId(sqlId)):ex.selectList(this.formatSqlId(sqlId), bean);
        } catch (Exception var5) {
            throw new DbException(var5);
        }
    }

    public T selectOne() throws DbException {
        return this.selectOne((Object)null);
    }

    public T selectOne(Object bean) throws DbException {
        return this.selectOne("find", bean);
    }

    public T selectOne(String sqlId, Object bean) throws DbException {
        return this.selectOne((SqlSessionTemplate)null, sqlId, bean);
    }

    public T selectOne(SqlSessionTemplate sessionTemplate, String sqlId, Object bean) throws DbException {
        try {
            SqlSession ex = this.getLocalSqlSession(sessionTemplate, false);
            return bean == null?ex.selectOne(this.formatSqlId(sqlId)):ex.selectOne(this.formatSqlId(sqlId), bean);
        } catch (Exception var5) {
            throw new DbException(var5);
        }
    }

    public <E> E selectBean() throws DbException {
        return this.selectBean((Object)null);
    }

    public <E> E selectBean(Object bean) throws DbException {
        return this.selectBean("find", bean);
    }

    public <E> E selectBean(String sqlId, Object bean) throws DbException {
        return this.selectBean((SqlSessionTemplate)null, sqlId, bean);
    }

    public <E> E selectBean(SqlSessionTemplate sessionTemplate, String sqlId, Object bean) throws DbException {
        try {
            SqlSession ex = this.getLocalSqlSession(sessionTemplate, false);
            return bean == null?ex.selectOne(this.formatSqlId(sqlId)):ex.selectOne(this.formatSqlId(sqlId), bean);
        } catch (Exception var5) {
            throw new DbException(var5);
        }
    }

    public Map<String, Object> selectMap() throws DbException {
        return this.selectMap((Object)null);
    }

    public Map<String, Object> selectMap(Object bean) throws DbException {
        return this.selectMap("find", bean);
    }

    public Map<String, Object> selectMap(String sqlId, Object bean) throws DbException {
        return this.selectMap((SqlSessionTemplate)null, sqlId, bean);
    }

    public Map<String, Object> selectMap(SqlSessionTemplate sessionTemplate, String sqlId, Object bean) throws DbException {
        try {
            SqlSession ex = this.getLocalSqlSession(sessionTemplate, false);
            return bean == null?(Map)ex.selectOne(this.formatSqlId(sqlId)):(Map)ex.selectOne(this.formatSqlId(sqlId), bean);
        } catch (Exception var5) {
            throw new DbException(var5);
        }
    }

    public List<Map<String, Object>> findSQL(String sql) throws DbException {
        return this.findSQL(sql, (Object[])null);
    }

    public List<Map<String, Object>> findSQL(String sql, Object[] beans) throws DbException {
        try {
            if(beans == null) {
                beans = new Object[0];
            }

            return this.jdbcTemplate.queryForList(sql, beans);
        } catch (Exception var4) {
            throw new DbException(var4);
        }
    }

    public Object findObjectSQL(String sql, Object[] beans, Class<?> clz) throws DbException {
        try {
            return this.jdbcTemplate.queryForObject(sql, beans, clz);
        } catch (Exception var5) {
            throw new DbException(var5);
        }
    }

    public Map<String, Object> findMapSQL(String sql, Object[] beans) throws DbException {
        try {
            return this.jdbcTemplate.queryForMap(sql, beans);
        } catch (Exception var4) {
            throw new DbException(var4);
        }
    }
}

